﻿using System;
using System.Linq;
using DataAccessLayer;
using DataTransferObject;
using System.Collections.Generic;

namespace BusinessLogicLayer
{
    public class NhaCCBLL : BLLBase
    {
        private readonly Idm_NhaCCDAL _dal;
        public NhaCCDto create = new NhaCCDto();
        public NhaCCBLL(string connectionString = "")
            : base(connectionString)
        {
            this._dal = new dm_NhaCCDAL(this.DatabaseFactory);
        }

        public IQueryable<NhaCCDto> Get(int facilityId, string key = "")
        {
            var query = this._dal.GetAll()
                .Where(e => e.Id == facilityId)
                .Select(e => new NhaCCDto
                {
                    Id = e.Id,
                    TenNCC = e.TenNCC,
                });

            if (!string.IsNullOrEmpty(key))
            {
                query = query.Where(e => e.TenNCC.StartsWith(key));
            }
            return query;
        }
        public List<NhaCCDto> GetAll()
        {
            var query = this._dal.GetAll()
                .Select(e => new NhaCCDto
                {
                    Id = e.Id,
                    TenNCC = e.TenNCC,
                    DiaChi = e.DiaChi,
                    Fax = e.Fax,
                    ForeignName = e.ForeignName,
                    IdMaNCC = e.IdMaNCC,
                    IsKhongChon = e.IsKhongChon,
                    IsKhuVuc = e.IsKhuVuc,
                    IsNVL = e.IsNVL,
                    LocalName = e.LocalName,
                    Mail = e.Mail,
                    MaST = e.MaST,
                    NganHang = e.NganHang,
                    SoDT = e.SoDT,
                    TaiKhoan = e.TaiKhoan,
                    Website = e.Website
                });
            return query.ToList();
        }
        public void Update(NhaCCDto dto, int facilityId)
        {
            var entity = this._dal.GetAll().FirstOrDefault(e => e.Id == dto.Id && e.Id == facilityId);
            if (entity == null) throw new Exception("Đối tượng không tồn tại");
            entity.TenNCC = dto.TenNCC;
            entity.DiaChi = dto.DiaChi;
            entity.Fax = dto.Fax;
            entity.ForeignName = dto.ForeignName;
            entity.IdMaNCC = dto.IdMaNCC;
            entity.IsKhongChon = dto.IsKhongChon;
            entity.IsKhuVuc = dto.IsKhuVuc;
            entity.IsNVL = dto.IsNVL;
            entity.LocalName = dto.LocalName;
            entity.Mail = dto.Mail;
            entity.MaST = dto.MaST;
            entity.NganHang = dto.NganHang;
            entity.SoDT = dto.SoDT;
            entity.TaiKhoan = dto.TaiKhoan;
            entity.Website = dto.Website;
            this.SaveChanges();
        }
        public void Insert(NhaCCDto dto, int facilityId)
        {
            var entity = new dm_NhaCC();
            entity.TenNCC = dto.TenNCC;
            entity.DiaChi = dto.DiaChi;
            entity.Fax = dto.Fax;
            entity.ForeignName = dto.ForeignName;
            entity.IdMaNCC = dto.IdMaNCC;
            entity.IsKhongChon = dto.IsKhongChon;
            entity.IsKhuVuc = dto.IsKhuVuc;
            entity.IsNVL = dto.IsNVL;
            entity.LocalName = dto.LocalName;
            entity.Mail = dto.Mail;
            entity.MaST = dto.MaST;
            entity.NganHang = dto.NganHang;
            entity.SoDT = dto.SoDT;
            entity.TaiKhoan = dto.TaiKhoan;
            entity.Website = dto.Website;
            this._dal.Add(entity);
            this.SaveChanges();
            dto.Id = entity.Id;
        }
        public void Delete(int id)
        {
            var entity = this._dal.GetAll().FirstOrDefault(e => e.Id == id );
            if (entity == null) throw new Exception("Đối tượng không tồn tại");
            this._dal.Delete(entity);
            this.SaveChanges();
        }
    }
}
